package com.rytx.system.refund;

import com.rytx.common.core.domain.AjaxResult;
import com.rytx.system.domain.ArtOrderAfter;
import com.rytx.system.domain.ysp.ArtOrder;
import com.rytx.system.mapper.ArtOrderAfterMapper;
import com.rytx.system.mapper.ArtOrderMapper;
import com.rytx.system.service.IArtAfterLogService;
import com.rytx.system.service.IArtOrderAfterService;
import com.rytx.system.service.impl.ArtOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 购买订单退款
 * */
@Component
public class BuyAfterRefund implements IAfterRefund{
    @Autowired
    private ArtOrderServiceImpl artOrderService;
    @Autowired
    private ArtOrderMapper artOrderMapper;

    @Autowired
    private IArtOrderAfterService artOrderAfterService;//售后表
    @Override
    public AjaxResult afterRefund(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {

        //得到退款金额
        BigDecimal refundAmount = getRefundAmount(artOrderAfter, artOrder);
        artOrderAfter.setRefundMoney(refundAmount.toString());
        //保存售后记录
        saveAfterRefundRecord(artOrderAfter, artOrder);
        //退款
        boolean refund = refund(artOrderAfter, artOrder);
        if(!refund){
            return AjaxResult.error("退款失败");
        }

        return settle(artOrderAfter, artOrder);
    }
    //校验参数


    //计算补偿金额
    @Override
    public BigDecimal getCompensationAmount(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {
        String compensationAmount = artOrderAfter.getCompensationAmount();
        return new BigDecimal(compensationAmount);
    }
    //计算退款金额
    @Override
    public BigDecimal getRefundAmount(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {
        //订单金额 - 补偿金额 = 退款金额
        BigDecimal orderAmount = artOrder.getOrderAmount();
        BigDecimal compensationAmount = getCompensationAmount(artOrderAfter, artOrder);
        //补偿金额不能大于订单金额
        if(compensationAmount.compareTo(orderAmount) > 0){
           throw new RuntimeException("补偿金额不能大于订单金额");
        }
        BigDecimal subtract = orderAmount.subtract(compensationAmount);
        return subtract;
    }

    @Override
    public boolean refund(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {
        //将订单状态设置为退款
        updateOrderStatus(artOrderAfter,artOrder);
        artOrderAfter.setStatus(IArtAfterLogService.ORDER_STATUS_REFUNDING);//将售后订单设置为退款中
        artOrderAfterService.updateArtOrderAfter(artOrderAfter);
        // return  artOrderService.afterZmnRefund(artOrder.getOrderNo());
        return true;
    }

    @Override
    public void updateOrderStatus(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {
        ArtOrder update = new ArtOrder();
        update.setOrderNo(artOrder.getOrderNo());
        update.setOrderStatus("7");// 订单状态 3-退款申请
        int i = artOrderMapper.updateByOrderNo(update);
    }

    @Override
    public AjaxResult settle(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {

        return AjaxResult.success();
    }

    @Override
    public void saveAfterRefundRecord(ArtOrderAfter artOrderAfter, ArtOrder artOrder) {
        //保存售后记录
        artOrderAfter.setStatus(IArtAfterLogService.ORDER_STATUS_FINISH);
        artOrderAfterService.updateArtOrderAfter(artOrderAfter);
    }
}
